package com.situ.javase.week3.Day3;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;

import com.situ.javase.week3.Day2.Student;

public class ObjectIoDemo {
public static void main(String[] args) {
	Student student = new Student(1, "lisi", 1000.0);
	ObjectOutputStream objectOutputStream = null;
	FileOutputStream fileOutputStream = null;
	try {
		fileOutputStream = new FileOutputStream("student");
		objectOutputStream = new ObjectOutputStream(fileOutputStream);
		objectOutputStream.writeObject(student);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		if (objectOutputStream != null) {
			try {
				objectOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		if (fileOutputStream != null) {
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
}


	@Test
	public void testObjectInputStream() {
	ObjectInputStream objectInputStream = null;
	FileInputStream fileInputStream = null;
	try {
		fileInputStream = new FileInputStream("student");
		objectInputStream = new ObjectInputStream(fileInputStream);
		Object object = objectInputStream.readObject();
		Student student = (Student)object;
		System.out.println(student);
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		if (objectInputStream != null) {
			try {
				objectInputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (fileInputStream != null) {
			try {
				fileInputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
}